<?php
/*
 * @Created by: wangchengcheng
 * @Author: wangchengcheng
 * @Date: 2020-01-03 17:35:18
 * @LastEditors  : wangchengcheng
 * @LastEditTime : 2020-01-03 20:07:25
 */

namespace App\Models;

use Str;

/**
 * App\Models\Config
 *
 * @property int                                $id         主键
 * @property string                             $name       配置项键名
 * @property string|null                        $value      配置项键值 1表示开启 0 关闭
 * @property \Illuminate\Support\Carbon|null    $created_at
 * @property \Illuminate\Support\Carbon|null    $updated_at
 * @property string|null                        $deleted_at
 */
class Config extends Base
{
    /**
     * 如果 value 是 json 则转成数组
     * 
     * @param $value
     * 
     * @return mixed
     */
    public function getValueAttribute($value)
    {
        if(Str::isJsonArray($value)){
            return json_decode($value, true);
        }

        return $value;
    }

    /**
     * 如果value是数组则转成json
     */
    public function setValueAttribute($value)
    {
        $this->attributes['value'] = is_array($value) ? json_encode($value) : $value;
    }
}
